﻿@model ElectrosLtdApplication.Models.FilterModel
@*@{List<SelectListItem> products = ViewBag.products;}*@
@*@model Common.Views.OrdersView*@

@{
    ViewBag.Title = "Electros Ltd | Filter Transactions";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Filter Transactions</h2>
<script type="text/javascript">
    $(function () {
        $("#start").datepicker();
        $("#end").datepicker();
    });

    function filter() {
        var startdate = $('#start').val();
        var enddate = $('#end').val();
        var pid = $("#ddlProducts option:selected").val();

        if ((startdate == "") && (enddate == "") && (pid == "")) {
            alert("Please choose a product, start date and end date!");
        }
        else if (startdate == "") {
            alert("Please choose a start date!");
        }
        else if (enddate == "") {
            alert("Please choose an end date!");
        }
        else if (pid == "") {
            alert("Please choose a product!");
        }
        else {
            var param = { startDate: startdate, endDate: enddate, pId: pid };

            $.ajax({
                url: '@Url.Action("GetTransactions")',
                type: "GET",
                datatype: "html",
                UpdateTargetId: "divResult",
                data: param,
                error: function (xmlHttpRequest, errorText, thrownError) {
                    alert("An error has occurred.");
                },
                success: function (data) {
                    $('#divResult').empty();
                    $('#divResult').html(data);
                }
            });
        }
    }
</script>

@if (Model.Products.Count() > 0)
{
    @Html.Label("Product: ")
    @Html.DropDownListFor(model => model.ProductID, Model.Products, "--Select Product--", new { id = "ddlProducts" }) <br />

    @Html.Label("Start Date: ") <input type="text" id="start" /> <br />
    @Html.Label("End Date: ") <input type="text" id="end" /> <br />

    <button type="button" onclick="filter()">Filter Transactions</button>
    <br /> <br />

    <div id="divResult">
    </div>
}
else
{
    <div style="color: Red"><b>You have not purchased any products</b></div>
}